package aiguigu07.encapsulation;

/**
 * ClassName: AnimalTest
 * Package: aiguigu07.encapsulation
 * Description:
 *
 * @Author ZY
 * @Create 2024/10/10 21:26
 * @Version 1.0
 */
public class AnimalTest {
    public static void main(String[] args) {

        Animal animal1 = new Animal();
        animal1.name = "青蛙";
//        animal1.legs = 4;

        animal1.setLegs(2);
        animal1.setLegs(-2);

//        System.out.println("name= " + animal1.name + ",legs= " + animal1.legs);
        System.out.println("name= " + animal1.name + ",legs= " + animal1.getLegs());

        animal1.eat();

    }
}
class Animal{

    //private:私有化
    String name;//名字
    private int legs;//腿的个数

    //赋值legs的属性
    public void setLegs(int l){
        if (l >= 0 && l % 2 == 0){
            legs = l;
        }else{
            System.out.println("×");
        }
    }
    //获取legs的属性
    public int getLegs(){
        return legs;
    }

    public void eat(){
        System.out.println("吃");
    }
}